package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Result;
import com.powernode.domain.Notice;
import com.powernode.dto.NoticeDto;
import com.powernode.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("notice")
@Api(tags = "公告的管理接口")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    @RequestMapping("list.do")
    @ApiOperation("查")
    public Result list(NoticeDto noticeDto){
        try {
            PageInfo<Notice> pageInfo = noticeService.queryNoticeForPage(noticeDto);
            System.out.println(pageInfo.getList());
            return new Result(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }

    @RequestMapping("addNotice.do")
    @ApiOperation("增")
    public Result addNotice(Notice notice){
        try {
            notice.setCreateTime(new Date());
            noticeService.insertSelective(notice);
            return Result.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    @RequestMapping("updateNotice.do")
    @ApiOperation("改")
    public Result updateNotice(Notice notice){
        try {
            noticeService.updateByPrimaryKeySelective(notice);
            return Result.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteNoticeById.do")
    @ApiOperation("删")
    public Result deleteNoticeById(Integer id){
        try {
            noticeService.deleteByPrimaryKey(id);
            return Result.DEL_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.DEL_ERROR;
        }
    }
}
